package books.ThinkingInJava.src.Chapter12;

import books.ThinkingInJava.src.Tools.Output;

import java.io.IOException;

public class ThrowAgain {
    public static void main(String[] args) {
        ThrowAgain ta = new ThrowAgain();
        try {
            ta.g();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }

        try {
            ta.h();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    public void f() throws IOException {
        Output.println("f");
        throw new IOException();
    }

    public void g() throws Exception {
        try {
            f();
        } catch (Exception e) {
//            e.printStackTrace();
            throw e;
        }
    }

    public void h() throws Exception {
        try {
            f();
        } catch (Exception e) {
//            e.printStackTrace();
            throw (Exception) e.fillInStackTrace();  //重新定义异常的初始地点
        }
    }
/*  输出结果：
            f
            java.io.IOException
            at Chapter12.ThrowAgain.f(ThrowAgain.java:11)
            at Chapter12.ThrowAgain.g(ThrowAgain.java:16)
            at Chapter12.ThrowAgain.main(ThrowAgain.java:34)
            f
            java.io.IOException
            at Chapter12.ThrowAgain.h(ThrowAgain.java:27)
            at Chapter12.ThrowAgain.main(ThrowAgain.java:40)
    */
}
